<% require_bundle :dynarch_calendar %>
<h1>Listing work done for <%= @client.name %> </h1>

<%= form_tag(:action => "list") %>
<%= hidden_field 'client', 'short'  %></p>
<table>
 <tr>
  <td>Between:</td>
  <td><%= dynarch_date_select 'search_criteria', 'from_date' %></td>
  <td>&nbsp;</td>
 </tr>
 <tr>
  <td>And:</td>
  <td><%= dynarch_date_select 'search_criteria', 'to_date' %></td>
  <td><%= submit_tag(" Search ") %></td>
 </tr>
</table>
</form>

<table cellspacing="2" cellpadding="2">
  <tr>
    <th>When</th>
    <th>From</th>
    <th>Project</th>
    <th>Who</th>
    <th>Duration</th>
    <th>Description</th>
  </tr>
  
<%
  date = nil
  for worklog in @worklogs
    desc = shorten_description(worklog.description)
    shortened = desc.length < worklog.description.length
    new_date = fmt_date(worklog.done_at)
    if (! date.nil? && new_date == date)
      new_date = ""
    else
      date = new_date
    end
%>
  <tr>
    <td style="width: 150;"><%=h new_date %></td>
    <td><%= fmt_user_time(worklog.done_at) %></td>
    <td><%=h @projects[worklog.assignment.project_id-1].short %></td>
    <td><%=h @users[worklog.assignment.user_id].short %></td
    <td><%=h fmt_duration(worklog.duration) %></td>
    <td>
     <span><%= fmt_description(worklog.description, worklog.project.issue_tracker) %></span>
     <!--%= shortened ? link_to ('Details', :action => 'show', :id => worklog) : ""%-->
    </td>
  </tr>
<% end %>
  <!-- FIXME use style instead of empty rows ...-->
  <tr><td colspan="5"></td></tr>
  <tr>
    <td colspan="4" align="center"><strong>Total</strong></td>
    <td id="totalcell"><strong><%= fmt_duration(@sum) %></strong></td>
  </tr>
  <tr><td colspan="5"></td></tr>

<% if @users.length > 1 %>
  <tr><td colspan="5">Total per worker</td></tr>
<%   for user_id in @users.keys %>
  <tr>
    <td colspan="3" align="right">&nbsp;</td>
    <td colspan="1"><%=@users[user_id].short%></td>
    <td><strong><%= fmt_duration(@sums["user-"+user_id.to_s]) %></strong></td>
  </tr>
<%   end
   end %>

<%
  displayed_projects = 0
   for project in @projects
     project_id = project.id
     total = @sums["project-"+project_id.to_s]
     if ! total.nil?
       if (displayed_projects = displayed_projects + 1) == 1
 %>
  <tr><td colspan="5">Total per project</td></tr>
 <%    end %>
  <tr>
    <td colspan="2" align="right">&nbsp;</td>
    <td colspan="1"><%=@projects[project_id-1].short%></td>
    <td>&nbsp;</td>
    <td><strong><%= fmt_duration(@sums["project-"+project_id.to_s]) %></strong></td>
  </tr>
<%
    end
  end %>

</table>

<%= timezone_info %>

<!-- %= link_to 'Previous page', { :page => @worklog_pages.current.previous } if @worklog_pages.current.previous %-->
<!-- %= link_to 'Next page', { :page => @worklog_pages.current.next } if @worklog_pages.current.next %--> 
